<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :search.sync="search"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="workOrderNoSearch" slot-scope="{}">
        <el-input placeholder="请输入工单号" v-model="search.workOrderNo" clearable @change="handleEnterWorkOrderNo">
          <el-button slot="append" icon="el-icon-search" @click="handleOpenWorkOrderModel"></el-button
        ></el-input>
      </template>
      <template slot="partNoSearch" slot-scope="{}">
        <el-input placeholder="请输入产品编码" v-model="search.partNo" clearable @change="handleEnterWorkOrderNo">
          <el-button slot="append" icon="el-icon-search" @click="handleOpenPartNoModel"></el-button
        ></el-input>
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val)"></user-table-config>-->
      <!--      </template>-->
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" size="small" icon="el-icon-view" @click="viewDetail(row)">详情</el-button>
      </template>
    </avue-crud>
    <el-dialog custom-class="woProgressQueryDialog" :title="'详情'" append-to-body :visible.sync="showDialog" :close-on-click-modal="false" width="50%" @close="closeDialog">
      <el-tabs type="border-card" v-model="tabName" @tab-click="tabClick">
        <el-tab-pane label="彩盒机身" name="彩盒机身">
          <avue-crud
            v-if="tabName === '彩盒机身'"
            :option="tabsOption"
            :table-loading="tabsLoading"
            :data="tabsData"
            :page.sync="tabsPage"
            ref="crud"
            @search-change="tabsSearchChange"
            @search-reset="tabsSearchReset"
            @current-change="tabsCurrentChange"
            @size-change="tabsSizeChange"
            @refresh-change="tabsRefreshChange"
          ></avue-crud>
          <div style="display: flex; justify-content: space-between">
            <span>合计机身(个)：{{ totalFuselageNum }}</span>
            <span>校验成功(次)：{{ totalVerificationSuccessNum }}</span>
            <span>校验失败(次)：{{ totalVerificationFailNum }}</span>
          </div>
        </el-tab-pane>
        <el-tab-pane label="彩盒外箱" name="彩盒外箱">
          <avue-crud
            v-if="tabName === '彩盒外箱'"
            :option="tabsOption"
            :table-loading="tabsLoading"
            :data="tabsData"
            :page.sync="tabsPage"
            ref="crud"
            @search-change="tabsSearchChange"
            @search-reset="tabsSearchReset"
            @current-change="tabsCurrentChange"
            @size-change="tabsSizeChange"
            @refresh-change="tabsRefreshChange"
          ></avue-crud>
          <div style="display: flex; justify-content: space-between">
            <span>合计彩盒(个)：{{ totalFuselageNum }}</span>
            <span>校验成功(次)：{{ totalVerificationSuccessNum }}</span>
            <span>校验失败(次)：{{ totalVerificationFailNum }}</span>
          </div>
        </el-tab-pane>
        <el-tab-pane label="外箱栈板" name="外箱栈板">
          <avue-crud
            v-if="tabName === '外箱栈板'"
            :option="tabsOption"
            :table-loading="tabsLoading"
            :data="tabsData"
            :page.sync="tabsPage"
            ref="crud"
            @search-change="tabsSearchChange"
            @search-reset="tabsSearchReset"
            @current-change="tabsCurrentChange"
            @size-change="tabsSizeChange"
            @refresh-change="tabsRefreshChange"
          ></avue-crud>
          <div style="display: flex; justify-content: space-between">
            <span>合计外箱(个)：{{ totalFuselageNum }}</span>
            <span>校验成功(次)：{{ totalVerificationSuccessNum }}</span>
            <span>校验失败(次)：{{ totalVerificationFailNum }}</span>
          </div>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getSelectedRows" :key="timer" @newKey="newKey"></common-modal-template>
  </basic-container>
</template>

<script>
import { getList, getDetailList, getDetailStatistics } from '@/api/report/boxCheckReport';
import userTableConfig from '@/components/user-table-config/index.vue';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'ReportBoxCheckReport',
  components: {
    userTableConfig,
    CommonModalTemplate
  },
  mixins: [userConfig()],
  data() {
    return {
      search: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      modalName: '', //打开公用模态框名称
      tableOption: {}, //公用模态框配置信息
      timer: new Date().getTime(),
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        labelWidth: 150,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        columnBtn: false,
        searchLabelWidth: 100,
        menuWidth: 100,
        column: [
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: '产品型号',
            prop: 'partModel'
          },
          {
            label: '校验次数',
            prop: 'verificationTimes'
          },
          {
            label: '成功次数',
            prop: 'successVerificationTimes'
          },
          {
            label: '状态',
            prop: 'status',
            search: true,
            type: 'select',
            dicData: [
              { label: '正常', value: 1 },
              { label: '锁定', value: 2 },
              { label: '已解锁', value: 3 }
            ]
          },
          {
            label: '对比结果',
            prop: 'comparativeResult',
            search: true,
            type: 'select',
            dicData: [
              { label: '合格', value: 1 },
              { label: '不合格', value: 0 }
            ],
            html: true,
            formatter: (val) => {
              if (val.comparativeResult === 0) {
                return '<span class="text-red">' + '不合格' + '</span>';
              } else if (val.comparativeResult === 1) {
                return '<span class="text-green">' + '合格' + '</span>';
              }
            }
          }
        ]
      },
      currentRowId: '',
      showDialog: false,
      tabName: '彩盒机身',
      tabsLoading: false,
      tabsPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      tabsQuery: {},
      tabsData: [],
      tabsOption: {
        height: 400,
        tip: false,
        searchShow: true,
        searchSpan: 8,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: '机身条码',
            prop: 'innerSn'
          },
          {
            label: '彩盒条码',
            prop: 'outerSn'
          },
          {
            label: '检验结果',
            prop: 'verificationResult',
            type: 'select',
            dicData: [
              { label: '成功', value: 1 },
              { label: '失败', value: 0 }
            ],
            search: true,
            html: true,
            formatter: (val) => {
              if (val.verificationResult === 0) {
                return '<span class="text-red">' + '失败' + '</span>';
              } else if (val.verificationResult === 1) {
                return '<span class="text-green">' + '成功' + '</span>';
              }
            }
          },
          {
            label: '操作人',
            prop: 'createName'
          },
          {
            label: '操作时间',
            prop: 'createTime'
          }
        ]
      },
      totalFuselageNum: 0,
      totalVerificationSuccessNum: 0,
      totalVerificationFailNum: 0
    };
  },

  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {},
  methods: {
    viewDetail(row) {
      this.currentRowId = row.id;
      this.showDialog = true;
      this.tabName = '彩盒机身';
      this.tabClick();
    },
    closeDialog() {
      this.showDialog = false;
    },
    searchReset() {
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    handleOpenWorkOrderModel() {
      this.modalName = 'workOrderNoModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择工单号（双击进行选择）',
        url: '/api/mes-product/work-order/page',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            search: true
          },
          {
            label: '生产工单',
            prop: 'orderNo',
            search: true
          },
          {
            label: '工单类型',
            prop: 'orderType',
            type: 'select'
          },
          {
            label: '工单数量',
            prop: 'planQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: 'BOM版本',
            prop: 'bomVersion'
          },
          {
            label: '派工产线',
            prop: 'productionLineName'
          },
          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '制定人',
            prop: 'creatorName'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '待排程',
                value: 1
              },
              {
                label: '待生产',
                value: 2
              },
              {
                label: '生产中',
                value: 3
              },
              {
                label: '完工',
                value: 4
              },
              {
                label: '锁定',
                value: 5
              },
              {
                label: '强制完工',
                value: 6
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark'
          }
        ]
      };
    },
    openProductNoCommonModal() {
      this.modalName = 'workOrderNoModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择工单号（双击进行选择）',
        url: '/api/mes-product/work-order/page',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            search: true
          },
          {
            label: '生产工单',
            prop: 'orderNo',
            search: true
          },
          {
            label: '工单类型',
            prop: 'orderType',
            type: 'select'
          },
          {
            label: '工单数量',
            prop: 'planQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: 'BOM版本',
            prop: 'bomVersion'
          },
          {
            label: '派工产线',
            prop: 'productionLineName'
          },
          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '制定人',
            prop: 'creatorName'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '待排程',
                value: 1
              },
              {
                label: '待生产',
                value: 2
              },
              {
                label: '生产中',
                value: 3
              },
              {
                label: '完工',
                value: 4
              },
              {
                label: '锁定',
                value: 5
              },
              {
                label: '强制完工',
                value: 6
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark'
          }
        ]
      };
    },
    handleOpenPartNoModel() {
      this.modalName = 'productNoModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择产品信息（双击进行选择）',
        url: '/api/mes-admin/part-master/page?semifinished=true',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '类型',
            prop: 'categoryId',
            search: true,
            type: 'tree',
            dicUrl: '/api/mes-admin/part-category/tree',
            props: {
              label: 'title',
              value: 'id'
            }
          },
          {
            label: '编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '名称',
            prop: 'name',
            search: true
          },
          {
            label: '型号',
            prop: 'partModel'
          },
          {
            label: '描述',
            prop: 'description'
          },
          {
            label: '版本',
            prop: 'revision'
          }
        ]
      };
    },
    /**
     * 获取选择的模态框行数据
     */
    getSelectedRows(rowData) {
      switch (this.modalName) {
        case 'workOrderNoModal':
          this.search.workOrderNo = this._get(rowData, 'orderNo', '');
          break;
        case 'productNoModal':
          this.search.partNo = this._get(rowData, 'partNo', '');
          break;
        default:
          break;
      }
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    },
    tabClick() {
      let innerSn = this.findObject(this.tabsOption.column, 'innerSn');
      let outerSn = this.findObject(this.tabsOption.column, 'outerSn');
      if (this.tabName === '彩盒机身') {
        innerSn.label = '机身条码';
        outerSn.label = '彩盒条码';
      } else if (this.tabName === '彩盒外箱') {
        innerSn.label = '彩盒条码';
        outerSn.label = '外箱条码';
      } else {
        innerSn.label = '外箱条码';
        outerSn.label = '栈板条码';
      }
      let params = {
        mfgVerificationId: this.currentRowId,
        verificationType: this.tabName === '彩盒机身' ? 1 : this.tabName === '彩盒外箱' ? 2 : 3
      };
      this.tabsLoading = true;
      getDetailList(this.tabsPage.currentPage, this.tabsPage.pageSize, Object.assign(params, this.tabsQuery)).then((res) => {
        const data = res.data.data;
        this.tabsPage.total = data.total;
        this.tabsData = data.records;
        this.tabsLoading = false;
      });
      getDetailStatistics(params).then(
        (res) => {
          this.totalFuselageNum = this._get(res, 'data.data.totalFuselageNum', 0);
          this.totalVerificationSuccessNum = this._get(res, 'data.data.totalVerificationSuccessNum', 0);
          this.totalVerificationFailNum = this._get(res, 'data.data.totalVerificationFailNum', 0);
        },
        (error) => {
          this.totalFuselageNum = 0;
          this.totalVerificationSuccessNum = 0;
          this.totalVerificationFailNum = 0;
        }
      );
    },
    tabsSearchReset() {
      this.tabsQuery = {};
      this.tabsPage.currentPage = 1;
      this.tabClick();
    },
    tabsSearchChange(params, done) {
      this.tabsQuery = params;
      this.tabsPage.currentPage = 1;
      this.tabClick();
      done();
    },
    tabsCurrentChange(currentPage) {
      this.tabsPage.currentPage = currentPage;
      this.tabClick();
    },
    tabsSizeChange(pageSize) {
      this.tabsPage.pageSize = pageSize;
      this.tabClick();
    },
    tabsRefreshChange() {
      this.tabClick();
    }
  }
};
</script>

<style lang="scss">
.btn {
  padding: 9px 15px;
  font-size: 10px;
  height: 35px;
  background-color: #409eff !important;
  color: #cccccc !important;
}

.is-disabled {
  background-color: #fff !important;
  color: #cccccc !important;
}

.title {
  color: #000;
}

.title:hover {
  color: #000;
}

.el-transfer-button {
  display: flex;
  flex-direction: row;
  height: 100%;
  margin: 40px 12px 0;
  align-items: center;
}

.search-container {
  display: flex;
  justify-content: space-between;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.el-color-picker__trigger {
  height: 35px;
  width: 35px;
}
.groups {
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #dedede;
  .titleIcon {
    display: flex;
    align-items: center;
  }
}

.woProgressQueryDialog {
  .el-dialog__body {
    padding: 20px;
  }
  .avue-crud__menu {
    display: none;
  }
}
</style>
